// pages/card/index.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    back: 0,
    cardList: [],
    page:1,
    p_user_id:0,
    cardInfo: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    this.setData({  shopInfo: app.getShopInfo() })
    app.getUserInfo(function (user_info) {
      that.data.user_info = user_info;
    })
    this.data.shop_id = this.data.shopInfo.id; 
    this.loadCardList();
    if (options.back) {
      this.data.back = 1;
    }
  },
  loadCardList: function (reload) {
    var that = this;
    if (reload == 1) {
      this.setData({
        loading: 0,
        cardList: [],
        lastpage:0,
        page:1,
      });
    }
    if (this.data.loading == 1) {
      return;
    }
   
    this.data.loading = 1;
    app.api("mall/card/get", {
     // shop_id: this.data.shop_id,
      page: this.data.page,
      status:1,
      limit:30
    }, function (res) {
      that.data.loading = 0;
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      if (res.data.list.length==0){
        that.data.lastpage=1;
        return ;
      }
      that.data.page++;
     
      that.setData({
        cardList: that.data.cardList.concat(res.data.list),
      });
      // console.log("cardList",that.data.cardList);
    })

  },

  addCard: function (e) {
    var card_id = e.currentTarget.dataset.card_id;
    //领取卡券
    app.api("mall/card/addCard", {
      cardId: card_id,
      p_user_id: this.data.p_user_id
    }, function (res) {
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      my.confirm({
        title: '提示',
        content: '领取成功',
        confirmButtonText: '马上使用',
        cancelButtonText: '我的卡券',
        success: (result) => {
          if (res.confirm) {
            //console.log('用户点击确定')
            my.switchTab({
              url: '/pages/index/index',
            })
          } else if (res.cancel) {
            my.navigateTo({
              url: '/pages/card/index',
            })

          }
        },
      });

    
      // my.navigateTo({
      //   url: '/pages/card/index',
      // })
    })
  },
  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.loadCardList(1);
    my.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
    * 用户点击右上角分享
    */
  onShareAppMessage: function (res) {
    var that = this;
    this.btn_cancel();
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    var title = this.data.user_info.nickname + "分享了一张[" + that.data.cardInfo.name+"]优惠券,快来领取吧";
    return {
      title: title,
      path: '/pages/card/info?cardId=' + that.data.cardInfo.cardId + '&p_user_id=' + this.data.user_info.user_id,
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  btn_cancel: function () {
    this.setData({ popup: 0 });
  },
 
})